<template>
    <div class="tab1">
        <Spin size="large" fix v-if="Loading"></Spin>
        <Form ref="data" :model="data" :rules="rule" :label-width="160" inline :disabled="disabledForStop">
            <table style="width:100%;">
                <tr>
                    <td style="width:26%;" colspan="2">
                        <FormItem label="申请人证件号码" prop="azcp0001">
                            <Input v-model="data.azcp0001" placeholder="" readonly class="itemStyle"/>
                        </FormItem>
                    </td>
                    <td style="width:29%;" colspan="2">
                        <FormItem label="申请人证件类型" prop="azcp0002">
                            <dictSelect :sel_val='data.azcp0002' dictCode="certificatetype"
                                        :bindData.sync="data.azcp0002" showKeys="1" class="itemStyle"/>
                        </FormItem>
                    </td>
                    <td style="width:29%;" colspan="2">
                        <FormItem label="申请人姓名" prop="azcp0003">
                            <c-blank-input v-model="data.azcp0003" placeholder="请输入申请人姓名" class="itemStyle"
                                           :maxlength="30"/>
                        </FormItem>
                    </td>
                    <td style="width:13%;" rowspan="4">
                        <Upload action="" :accept="acceptImg" :before-upload="(file) => beforeUpload(file,'azcp0519')">
                            <img :src="data.azcp0519" alt="..." class="dbhead-img">
                        </Upload>
                    </td>
                </tr>
                <tr>
                    <td colspan="4">
                        <FormItem label="户籍地行政区划" prop="azdf0006">
                            <area-linkage :areaCode.sync="data.azdf0006" :countyDisable=false :streetDisable=false
                                          :communityDisable=false></area-linkage>
                        </FormItem>
                    </td>
                    <td colspan="2">
                        <FormItem label="户籍地邮政编码" prop="azdf0009">
                            <Input v-model="data.azdf0009" placeholder="" class="itemStyle" maxlength="10"/>
                        </FormItem>
                    </td>
                </tr>
                <tr>
                    <td colspan="4">
                        <FormItem label="户籍地详细地址" prop="azdf0500">
                            <Input v-model="data.azdf0500" placeholder="" :disabled="azdf0500Disable" maxlength="100"
                                   class="itemLongStyle"/>
                        </FormItem>
                    </td>
                    <td colspan="2">
                        <FormItem label="常住地邮政编码" prop="azdf0502">
                            <Input v-model="data.azdf0502" placeholder="" class="itemStyle" maxlength="10"/>
                        </FormItem>
                    </td>
                </tr>
                <tr>
                    <td colspan="6">
                        <FormItem label="是否本市居住" prop="azdf0505">
                            <c-dict-select v-model="data.azdf0505" dictCode="yesorno" :width="180"/>
                        </FormItem>
                    </td>
                </tr>
                <tr v-if="data.azdf0505=='0'">
                    <td colspan="6">
                        <FormItem label="外市居住区划" prop="azdf0507">
                            <Ss01003Tag :bindshengjiData.sync="data.azdf0506"
                                        :bindshijiData.sync="data.azdf0507"
                                        :shengji_value="data.azdf0506"
                                        :shiji_value="data.azdf0507"
                                        style="width: 180px">
                            </Ss01003Tag>
                        </FormItem>
                    </td>
                </tr>
                <tr v-if="data.azdf0505=='1'">
                    <td colspan="4">
                        <FormItem label="常住地行政区划" prop="azdf0501">
                            <area-linkage :areaCode.sync="data.azdf0501" :countyDisable=false :streetDisable=false
                                          :communityDisable=false></area-linkage>
                        </FormItem>
                    </td>
                    <td colspan="2">
                        <FormItem label="居住地类型" prop="azdf0512">
                            <c-dict-select v-model="data.azdf0512" dictCode="live_type" :width="180"
                                           :disabled="azdf0512Disabled"/>
                        </FormItem>
                    </td>
                </tr>
                <tr v-if="data.azdf0512=='01'&&data.azdf0505=='1'">
                    <td style="width:26%;" colspan="2">
                        <FormItem label="小区名称" prop="azdf0513">
                            <Select v-model="data.azdf0513" class="itemStyle" filterable>
                                <Option :value="i.cmcg0002" v-for="i in azdf0513List" :key="i.id">{{i.cmcg0002}}
                                </Option>
                            </Select>
                        </FormItem>
                    </td>
                    <td style="width:29%;" colspan="2">
                        <FormItem label="楼栋号" prop="azdf0514">
                            <Input v-model="data.azdf0514" placeholder="请输入楼栋号" class="itemStyle" maxlength="20"/>
                        </FormItem>
                    </td>
                    <td style="width:29%;" colspan="3">
                        <FormItem label="户室门牌号" prop="azdf0515">
                            <Input v-model="data.azdf0515" placeholder="请输入户室门牌号" class="itemStyle" maxlength="20"/>
                        </FormItem>
                    </td>
                </tr>
                <tr v-if="data.azdf0512=='02'&&data.azdf0505=='1'">
                    <td style="width:29%;" colspan="7">
                        <FormItem label="村小组号" prop="azdf0516">
                            <Input v-model="data.azdf0516" placeholder="请输入村小组号" class="itemStyle" maxlength="20"/>
                        </FormItem>
                    </td>
                </tr>
                <tr>
                    <td colspan="4">
                        <FormItem label="常住地详细地址" prop="azdf0008">
                            <Input v-model="data.azdf0008" placeholder="" :disabled="data.azdf0505=='1'" class="itemLongStyle"
                                   maxlength="100"/>
                        </FormItem>
                    </td>
                    <td colspan="2">
                        <FormItem label="联系人类型" prop="azdf0510">
                            <c-dict-select v-model="data.azdf0510" dictCode="contact_type" :width="180"
                                           @change="changeContactType"/>
                        </FormItem>
                    </td>
                </tr>
                <tr>
                    <td colspan="4">
                        <FormItem label="所属地行政区划" prop="azdf0007">
                            <area-linkage :areaCode.sync="data.azdf0007" :countyDisable=countyState
                                          :streetDisable=streetState
                                          :communityDisable=communityState></area-linkage>
                        </FormItem>
                    </td>
                    <td colspan="3">
                        <FormItem label="联系人姓名" prop="azdf0511">
                            <Input v-model="data.azdf0511" placeholder="请输入联系人姓名" class="itemStyle" maxlength="30"/>
                        </FormItem>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <FormItem label="是否有非保障人员" prop="ssca0087">
                            <dictSelect :sel_val='data.ssca0087' dictCode="yesorno" :bindData.sync="data.ssca0087"
                                        class="itemStyle"/>
                        </FormItem>
                    </td>
                    <td colspan="2">
                        <FormItem label="是否有赡(抚、扶)养人" prop="ssca0088">
                            <dictSelect :sel_val='data.ssca0088' dictCode="yesorno" :bindData.sync="data.ssca0088"
                                        class="itemStyle"/>
                        </FormItem>
                    </td>
                    <td colspan="3">
                        <FormItem label="联系方式" prop="azdf0011">
                            <Input v-model="data.azdf0011" placeholder="" class="itemStyle" maxlength="30"/>
                        </FormItem>
                    </td>
                </tr>
            </table>
        </Form>
        <div style="height:40px;line-height:40px;margin-top:10px;text-align: center ">
            <Button type="primary" @click="checkData()" :disabled="disabledForStop">{{checkBtnName}}</Button>
            <Button type="primary" @click="nextStep('data')" :disabled="validateStatus" :loading="btnLoading"
                    style="margin-left:20px">下一步
            </Button>
        </div>

        <Modal v-model="ss00011ModelShow" style="margin-top:20px;" :title="yzModalTitle" :mask-closable="false"
               width="800">
            <Ss00011Check
                    :azcp0001="data.azcp0001"
                    :ssca0002="$parent.familyInfo.ssca0002"
                    :ssca0016="$parent.familyInfo.ssca0016"
                    :azcp0003="data.azcp0003"
                    :ss12001Id="data.ss12001Id"
                    :ss17001Id="data.ss17001Id"
                    :ss17002Id="data.ss17002Id"
                    :isRequestBackstage="ss00011ModelShow"
                    :batchNumber.sync="batchNumber"
                    :validateInfo.sync="validateInfo"
                    :validateStatus.sync="validateStatus"
                    :azdf0500.sync="data.azdf0500"
                    :checkBtnName.sync="checkBtnName"
                    :azdf0500Disable.sync="azdf0500Disable"
            />
        </Modal>
    </div>
</template>

<script>
    import pic_header from '@/assets/images/header.jpeg'
    import dictSelect from '@/components/dictSelect/dictSelect.vue'
    import {postAction} from '@/api/request'
    import Ss00011Check from '@/components/system/ss00011-check'
    import {CommonMixin} from '@/views/common/js/CommonMixin'
    import Ss01003Tag from '@/components/system/ss01003-select'// 户籍区划功能

    export default {
        name: 'application1',
        mixins: [CommonMixin],
        components: {
            dictSelect,
            Ss00011Check,
            Ss01003Tag
        },
        data() {
            return {
                Loading: false,
                btnLoading: false,
                disabledForStop: false, //停救业务，页面不可编辑
                countyState: false,
                streetState: false,
                communityState: false,
                currentTab: 0,
                familyInfo: {},
                azdf0512Disabled: true,
                azdf0513List: [],
                fieldsToValidate:  ['azcp0001', 'azcp0002', 'azcp0003', 'azdf0006','azdf0505',  'azdf0501', 'azdf0512', 'azdf0007', 'azdf0011', 'ssca0087', 'ssca0088', 'azdf0510', 'azdf0511'],
                rule: {
                    azcp0001: [
                        {required: true, message: '请填写申请人证件号码', trigger: 'blur'}
                    ],
                    azcp0002: [
                        {required: true, message: '请选择证件类型', trigger: 'blur'}
                    ],
                    azcp0003: [
                        {required: true, message: '请填写申请人姓名', trigger: 'blur'}
                    ],
                    azdf0006: [
                        {required: true, message: '请选择户籍地行政区划', trigger: 'blur'},
                        {pattern: /^((?!000$).)*$/, message: '请选择完整户籍地行政区划', trigger: 'blur'}
                    ],
                    azdf0501: [
                        {required: true, message: '请选择常住地行政区划', trigger: 'blur'},
                        {pattern: /^((?!000$).)*$/, message: '请选择完整常住地行政区划', trigger: 'blur'}
                    ],
                    azdf0505: [
                        {required: true, message: '请选择是否本市居住', trigger: 'blur'}
                    ],
                    azdf0507: [
                        {required: true, message: '请选择外市居住区划', trigger: 'blur'}
                    ],
                    azdf0500: [
                        {required: true, message: '请填写户籍地详细地址', trigger: 'blur'}
                    ],
                    azdf0008: [
                        {required: true, message: '请填写常住地详细地址', trigger: 'blur'}
                    ],
                    azdf0007: [
                        {required: true, message: '请选择所属地行政区划', trigger: 'blur'},
                        {pattern: /^((?!000$).)*$/, message: '请选择完整所属地行政区划', trigger: 'blur'}
                    ],
                    azdf0011: [
                        {required: true, message: '请填写联系方式', trigger: 'blur'},
                        {pattern: /^[0-9]{5,20}$/, message: '请填写正确的联系电话', trigger: 'blur'}
                    ],
                    ssca0087: [
                        {required: true, message: '请选择是否有非保障人员', trigger: 'blur'}
                    ],
                    ssca0088: [
                        {required: true, message: '请选择是否有赡(抚、扶)养人', trigger: 'blur'}
                    ],
                    azdf0510: [
                        {required: true, message: '请选择联系人类型', trigger: 'change'}
                    ],
                    azdf0511: [
                        {required: true, message: '请输入联系人姓名', trigger: 'blur'}
                    ],
                    azdf0512: [
                        {required: true, message: '请选择居住地类型', trigger: 'change'}
                    ],
                    azdf0513: [
                        {required: true, message: '请选择小区名称', trigger: 'change'}
                    ],
                    azdf0514: [
                        {required: true, message: '请输入楼栋号', trigger: 'blur'}
                    ],
                    azdf0515: [
                        {required: true, message: '请输入户室门牌号', trigger: 'blur'}
                    ],
                    azdf0516: [
                        {required: true, message: '请输入村小组号', trigger: 'blur'}
                    ]
                },
                url: {
                    submit: '/api/ac/subsistence/ss17001Service/createDb17Step1Submit',
                    load: '/api/ac/subsistence/ss17000Service/updateSs17001Data',
                    areaInfo: '/api/ac/subsistence/ss11001Service/getUserAreaInfo',
                    backWriteSs00011: '/api/ac/subsistence/ss00011Service/saveBackWriteSs00011'
                },
                data: {
                    azcp0002: '',
                    azcp0003: '',
                    azdf0006: '',
                    // azdf0010: '',
                    azdf0011: '',
                    azdf0500: '',
                    azdf0501: '',
                    azdf0007: '',
                    azdf0008: '',
                    ssca0002: '',
                    ssca0087: '',
                    ssca0088: '',
                    azcp0001: '',
                    azcp0519: pic_header,
                    azcp0578: '0', //是否通过低保事前验证  0否  1是
                    ss17001Id: '',
                    ss17002Id: '',
                    ss12001Id: '',
                    azdf0510: '',
                    azdf0511: '',
                },
                checkBtnName: '人员信息(未查验)',
                validateStatus: true, //查验状态 true 未查验或查验异常 false 查验通过
                azdf0500Disable: false, //户籍地址组件是否可用，根据事前查验接口调用结果，如有户籍地址  true 反之 false
                //批次号
                batchNumber: '',
                validateInfo: '',
                //事前查验标题
                yzModalTitle: '',
                ss00011ModelShow: false,
            }
        },
        methods: {
            resetForm() {
                let that = this
                that.data.azcp0003 = null
                that.data.azdf0009 = null
                // that.data.azdf0010 = null;
                that.data.azdf0502 = null
                that.data.azdf0008 = null
                that.data.azdf0500 = null
                that.data.azdf0011 = null
                that.data.ssca0087 = null
                that.data.ssca0088 = null
                that.data.azcp0578 = '0'
                that.validateStatus = true
                that.checkBtnName = '人员信息(未查验)'
            },
            loadStep1Data(data) { //从后面步骤返回第一步时调用
                let that = this
                let url = that.url.load
                that.familyInfo = data
                let dataParam = {}
                dataParam.azcp0001 = data.azcp0001
                dataParam.ss12001Id = data.ss12001Id
                dataParam.ss16001Id = data.ssca0001
                dataParam.ssca0002 = that.$parent.familyInfo.ssca0002
                dataParam.bgType = that.$parent.familyInfo.ssca0016
                dataParam.isPush = 'no'
                that.Loading = true
                postAction(url, dataParam).then((res) => {
                    if (res.data.data) {
                        if (res.data.data.code == 1) {
                            let record = res.data.data.data
                       console.log(record)
                            that.data = Object.assign({}, record)
                            if (that.data.azcp0519 == null) {
                                that.data.azcp0519 = pic_header
                            }
                            if (that.data.azcp0578 == '1') { //已完成验证
                                that.validateStatus = false
                            } else {
                                that.validateStatus = true
                            }
                            that.$Message.success(res.data.data.msg)
                            //如是停救、迁移,页面不可编辑
                            if (that.$parent.familyInfo.ssca0016 == '300' || that.$parent.familyInfo.ssca0016 == '500' || that.$parent.familyInfo.ssca0016 == '501') {
                                that.disabledForStop = true
                                that.validateStatus = false
                                that.data.azcp0578 = '1'
                            }

                        } else {
                            that.$Message.error(res.data.data.msg)
                        }
                    } else {
                        that.$Message.error(res.data.errmsg)
                    }
                    console.log("then end");
                }).finally(() => {
                    console.log("finally end");
                    that.Loading = false
                })
            },

            checkData() {
                let that = this
                let fieldsToValidate = ['azcp0001', 'azcp0003']
                let validate = true
                let err_msg = []
                for (let item of fieldsToValidate) {
                    that.$refs['data'].validateField(item, (error) => {
                        if (error) {
                            validate = false
                            err_msg.push(error)
                        }
                    })
                }
                if (validate) {
                    that.yzModalTitle = '事前查验-姓名：【' + that.data.azcp0003 + '】 身份证号码：【' + that.data.azcp0001 + '】'
                    that.ss00011ModelShow = true
                    that.azdf0500Disable = false
                } else {
                    that.$Message.error(err_msg[0])
                    return
                }
            },
            backWriteSs00011: function (ss12001Id, batchNumber) {  //查验结果关联业务id
                //验证是否进行过查验
                let dataParam = {
                    ss12001Id: ss12001Id,
                    batchNumber: batchNumber,
                }
                postAction(this.url.backWriteSs00011, dataParam).then((res) => {
                })
            },
            async nextStep(name) {
                this.data.azcp0519url = this.header
                let that = this
                let validate = true
                let err_msg = []
                that.btnLoading = true

                for (let item of that.fieldsToValidate) {
                    that.$refs[name].validateField(item, (error) => {
                        if (error) {
                            validate = false
                            err_msg.push(error)
                        }
                    })
                }
                if (validate) {
                    if (this.data.azdf0505 == '1') {
                        var azdf0501 = this.data.azdf0501
                        var areaName = await this.getAreaNameByCode(azdf0501)
                        var azdf0008 = areaName
                        if (this.data.azdf0512 == '01') {
                            this.data.azdf0516 = ''
                            azdf0008 += ' ' + this.data.azdf0513 + ' ' + this.data.azdf0514 + ' ' + this.data.azdf0515
                        } else {
                            this.data.azdf0513 = ''
                            this.data.azdf0514 = ''
                            this.data.azdf0515 = ''
                            azdf0008 += ' ' + this.data.azdf0516
                        }
                        this.data.azdf0008 = azdf0008
                    };
                    let dataParam = {}
                    dataParam = Object.assign({}, that.data)
                    if(dataParam.azcp0519==pic_header){
                        dataParam.azcp0519=null;
                    }
                    let url = that.url.submit
                    //变更类型
                    dataParam.handleType = that.$parent.familyInfo.ssca0016
                    dataParam.renewSs12001 = '0'
                    postAction(url, dataParam).then((res) => {
                        if (res.data.data) {
                            if (res.data.data.code == 1) {
                                let info = res.data.data
                                that.$Message.success(res.data.data.msg)
                                that.$parent.familyInfo.prsnid = info.data.prsnid
                                that.$parent.familyInfo.ss12001Id = info.data.ss12001Id
                                that.$parent.familyInfo.ss17001Id = info.data.id
                                //针对类型转换，如果当前存储救助类型不等于返回类型，更新存储
                                if (that.$parent.familyInfo.ssca0002 != info.data.ssca0002) {
                                    that.$parent.familyInfo.ssca0002 = info.data.ssca0002
                                }
                                that.$parent.familyInfo.ssca0087 = that.data.ssca0087
                                that.$parent.familyInfo.ssca0088 = that.data.ssca0088
                                that.backWriteSs00011(info.data.ss12001Id, that.batchNumber)
                                that.familyInfo.azdf0007 = that.data.azdf0007
                                that.$emit('nextStep', that.familyInfo)
                            } else {
                                that.$Message.error(res.data.data.msg)
                            }
                        } else {
                            that.$Message.error('数据交互异常')
                        }
                    }).finally(() => {
                        that.btnLoading = false
                    })
                } else {
                    that.$Message.error(err_msg[0])
                    that.btnLoading = false
                    return
                }
            },
            setAreaState() {
                let that = this
                let url = that.url.areaInfo
                let dataParam = {}
                that.Loading = true
                postAction(url, dataParam).then((res) => {
                    if (res.data.data) {
                        if (res.data.data.code == 1) {
                            let record = res.data.data.data
                            let arealevel = record.arealevel
                            if (arealevel == 4) {
                                that.countyState = true
                                that.streetState = true
                                that.communityState = true
                            } else if (arealevel == 3) {
                                that.countyState = true
                                that.streetState = true
                                that.communityState = false
                            } else if (arealevel == 2) {
                                that.countyState = true
                                that.streetState = false
                                that.communityState = false
                            }
                        } else {
                        }
                    } else {
                        that.$Message.error('数据交互异常，个人属地信息载入异常')
                    }
                }).finally(() => {
                    that.Loading = false
                })
            },
            changeContactType() {
                let that = this
                if (!that.isEmpty(that.data.azcp0003)) {
                    if (that.data.azdf0510 == '01') {
                        that.data.azdf0511 = that.data.azcp0003
                    } else {
                        that.data.azdf0511 = undefined
                    }
                } else {
                    that.$Message.error('请输入申请人姓名')
                }
            },
            //获取小区名称列表
            getAzdf0513List() {
                var azdf0501 = this.data.azdf0501
                this.Loading = true
                postAction('/api/ac/subsistence/ss01004Service/findList', {cmcg0001: azdf0501}).then((res) => {
                    if (res.data.errcode != 0) {
                        this.$Message.error(res.data.errmsg)
                    } else {
                        this.azdf0513List = res.data.data
                    }
                }).finally(() => {
                    this.Loading = false
                })
            },
            loadImg(response, file, name, id) {
                if (response.errcode == 0) {
                    this.$set(this.data, name, response.data)
                } else {
                    this.$Message.error('上传失败！')
                }
                this.Loading = false
            },
        },
        created() {
            this.setAreaState()
        },
        mounted() {
            let that = this
            let params = that.$parent.familyInfo
            if (typeof (params.azcp0001) != 'undefined') {
                that.loadStep1Data(params)
            } else {
                that.$refs['data'].resetFields()
            }
        },
        watch: {
            'data.azcp0003': {
                handler(newName, oldName) {
                    if (oldName != '' && newName != oldName) {
                        this.validateStatus = true
                    }
                },
                immediate: true,
            },
            'data.azdf0501': function (val, oldVal) {
                let reg = /^((?!000$).)*$/;
                if (oldVal&&reg.test(oldVal)) {
                    this.data.azdf0008 = '';
                    this.data.azdf0512 = ''
                    this.data.azdf0513 = ''
                    this.data.azdf0514 = ''
                    this.data.azdf0515 = ''
                    this.data.azdf0516 = ''
                }
                if (reg.test(val)) {
                    this.azdf0512Disabled = false
                } else {
                    this.azdf0512Disabled = true
                }
            },
            'data.azdf0512': function (val) {
                this.fieldsToValidateRemove(['azdf0513', 'azdf0514', 'azdf0515', 'azdf0516'])
                if (val == '01') {
                    this.fieldsToValidateAdd(['azdf0513', 'azdf0514', 'azdf0515'])
                    this.getAzdf0513List()
                } else if (val == '02') {
                    this.fieldsToValidateAdd(['azdf0516'])
                }
            },
            'data.azdf0505': function (val, oldVal) {
                this.fieldsToValidateRemove(['azdf0008', 'azdf0501', 'azdf0512', 'azdf0513', 'azdf0514', 'azdf0515', 'azdf0516', 'azdf0507']);
                if (oldVal) {
                    this.data.azdf0501 = '';
                    this.data.azdf0507 = '';
                    this.data.azdf0506 = '';
                }
                if (val == '1') {
                    this.fieldsToValidateAdd(['azdf0512', 'azdf0501']);
                } else if (val == '0') {
                    this.fieldsToValidateAdd(['azdf0008', 'azdf0507']);
                }
            },
            validateStatus(newVal, oldVal) {
                if (!newVal) {
                    this.checkBtnName = '人员信息(已查验)'
                    this.data.azcp0578 = '1'  //事前校验通过
                } else {
                    this.checkBtnName = '人员信息(未查验)'
                }
            },
            'data.azdf0510': function () {
                if (this.data.azdf0510 == '01'){
                    this.editable = true
                }else{
                    this.editable = false
                }
            }
        }

    }

</script>

<style scoped>
    .dbhead-img {
        height: 160px;
        width: 120px;
        margin-left: 10px;
    }

    /deep/ .ivu-form-item-error-tip {
        display: none;
    }

    .ivu-form-item {
        margin-bottom: 0px;
    }

    .itemStyle {
        width: 180px
    }

    .itemLongStyle {
        width: 450px
    }
</style>
